package com.example.provider.com.sjf.controller;


import com.example.provider.com.sjf.biz.ProviderBizImpl;
import com.example.provider.com.sjf.pojo.Providers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ProviderController {
    @Autowired
    private ProviderBizImpl providerBiz;

    @RequestMapping("info")
    public String info(@RequestParam("userName") String userName, HttpSession session) {
        session.setAttribute("userName", userName);
        return "redirect:all";
    }

    @RequestMapping("all")
    public String getAll(@RequestParam(value = "pageno", defaultValue = "1") int pageno, Model model) {
        PageInfo<Providers> all = this.providerBiz.getAll(pageno);
        model.addAttribute("page", all);
        return "main";
    }

    @GetMapping("shows")
    public String show(Long id, Model model) {
        Providers providers = this.providerBiz.show(id);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        providers.setCreationDates(sd.format(providers.getCreationDate()));
        model.addAttribute("providers", providers);
        return "show";
    }

    @GetMapping("dele")
    public String delete(Long id, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        int re = this.providerBiz.deleteByPrimaryKey(id);
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (re > 0) {
            out.print("<script type=\"text/javascript\" charset=\"utf-8\">\n" +
                    "    alert(\"删除成功\")\n" +
                    "    location.href =\"all\";\n" +
                    "</script>");
            out.flush();
            out.close();
        }
        return "redirect:all";
    }

    @GetMapping("inser")
    public String inser() {
        return "insert";
    }

    @PostMapping("inserts")
    public String insert(Providers providers) {
        providers.setCreationDate(new Date());
        int i = this.providerBiz.insert(providers);
        return "redirect:all";
    }

    @GetMapping("updat")
    public String updat(Long id, Model model) {
        Providers providers = this.providerBiz.selByid(id);
        model.addAttribute("providers", providers);
        return "update";
    }

    @PostMapping("update")
    public String update(Providers providers) {
        providers.setCreationDate(new Date());
        int i = this.providerBiz.updateByPrimaryKeySelective(providers);

        return "redirect:all";
    }

    @GetMapping("selM")
    public String selM(Providers providers, Model model) {

        if (providers.getProCode() != "" || providers.getProName() != "") {
            List<Providers> list = this.providerBiz.selM(providers);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < list.size(); i++) {
                Date date = list.get(i).getCreationDate();
                String s = sd.format(date);
                list.get(i).setCreationDates(s);
            }

            model.addAttribute("list", list);
            return "sell";
        }
        return "redirect:all";
    }
}
